package com.jingge.home.util.wx;

import me.chanjar.weixin.cp.bean.message.WxCpXmlOutImageMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutVoiceMessage;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
import org.slf4j.helpers.FormattingTuple;

/**
 * 微信消息构建器
 *
 * @author Yongxiang
 * @date 2025-06-04 17:07
 * @email 1158055613@qq.com
 */
public class WxMessageBuilder {
	public static WxCpXmlOutTextMessage buildTextMessage(String content,
	                                                 String from,
	                                                 String to,
	                                                 Object... param) {
		return WxCpXmlOutTextMessage.TEXT().content(formatMsgLikeSlf4j(content, param)).fromUser(from).toUser(to).build();
	}

	public static String toXml(WxCpXmlOutMessage message) {
		if (message instanceof WxCpXmlOutTextMessage) {
			return XStreamTransformer.toXml(WxCpXmlOutTextMessage.class, (WxCpXmlOutTextMessage) message);
		} else if (message instanceof WxCpXmlOutImageMessage) {
			return XStreamTransformer.toXml(WxCpXmlOutImageMessage.class, (WxCpXmlOutImageMessage) message);
		} else if (message instanceof WxCpXmlOutVoiceMessage) {
			return XStreamTransformer.toXml(WxCpXmlOutVoiceMessage.class, (WxCpXmlOutVoiceMessage) message);
		}
		return null;
	}

	private static String formatMsgLikeSlf4j(String appendMsgPattern,
	                                         Object... appendLogArguments) {
		FormattingTuple ft = org.slf4j.helpers.MessageFormatter.arrayFormat(appendMsgPattern, appendLogArguments);
		return ft.getMessage();
	}
}
